package com.zxx.order.presenter

import com.zqq.baselibrary.ext.excute
import com.zqq.baselibrary.http.rx.BaseSubscriber
import com.zqq.baselibrary.presenter.BasePresenter
import com.zxx.order.presenter.iview.OrderListView
import com.zxx.order.service.OrderService
import com.zxx.order.service.bean.Order
import com.zxx.order.service.bean.OrderGoods
import com.zxx.order.service.bean.ShipAddress
import javax.inject.Inject

/**
 * 订单列表Presenter
 * wc
 * @author zqq on 2019/9/7.
 */
class OrderListPresenter @Inject constructor() : BasePresenter<OrderListView>() {

    @Inject
    lateinit var orderService: OrderService

    //根据订单状态获取订单列表
    fun getOrderList(orderStatus: Int) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()

        orderService.getOrderList(orderStatus)
                .excute(object : BaseSubscriber<MutableList<Order>?>(mView) {
                    override fun onNext(t: MutableList<Order>?) {
                        super.onNext(t)
                        mView.onGetOrderListResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        var dataList =
                                if (orderStatus == 2) {
                                    loadListData()
                                } else {
                                    loadMoreListData()
                                }
                        mView.onGetOrderListResult(dataList)
                    }
                }, lifecycleProvider)
    }

    //多个商品
    private fun loadMoreListData(): MutableList<Order> {
        var list = mutableListOf<Order>()
        var address = ShipAddress(1, "name", "136***78", "杭州", 11)
        var listOrderGoods = mutableListOf<OrderGoods>()
        var banner = "http://pic32.nipic.com/20130902/13395269_103525238102_2.jpg"
        var sku1 = "https://imgsa.baidu.com/baike/pic/item/18d8bc3eb13533faec829583a6d3fd1f41345b62.jpg"

        listOrderGoods.add(OrderGoods(1, 11, banner, banner, 99, 10, "红色", 11))
        listOrderGoods.add(OrderGoods(1, 21, sku1, banner, 99, 5, "栏色", 12))

        list.add(Order(1, 11, address, 11L, 3, listOrderGoods))
        list.add(Order(2, 21, address, 21L, 2, listOrderGoods))

        return list
    }

    //模拟加载数据
    private fun loadListData(): MutableList<Order> {
        var list = mutableListOf<Order>()
        var address = ShipAddress(1, "name", "136***78", "杭州", 11)
        var listOrderGoods = mutableListOf<OrderGoods>()
        var banner = "http://pic32.nipic.com/20130902/13395269_103525238102_2.jpg"
        listOrderGoods.add(OrderGoods(1, 11, "商品描述", banner, 99, 10, "红色", 11))

        list.add(Order(1, 11, address, 11L, 2, listOrderGoods))

        return list
    }

    //订单确认收货
    fun confirmOrder(orderId: Int) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        orderService.confirmOrder(orderId)
                .excute(object : BaseSubscriber<Boolean>(mView) {
                    override fun onNext(t: Boolean) {
                        super.onNext(t)
                        mView.onConfirmOrderResult(t)
                    }

                }, lifecycleProvider)
    }

    //取消订单
    fun cancelOrder(orderId: Int) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        orderService.cancelOrder(orderId)
                .excute(object : BaseSubscriber<Boolean>(mView) {
                    override fun onNext(t: Boolean) {
                        super.onNext(t)
                        mView.onCancelOrderResult(t)
                    }

                }, lifecycleProvider)
    }


}